<template>
  <div>
    <el-dialog
      title="设置领导"
      :visible.sync="drawer"
      :close-on-click-modal="false"
      width="1200px"
      @close="CloseRoles()"
      @open="openhander"
    >
      <tranformcom
        :id="overallId"
        :loading="loading"
        :treedatalist="list"
        :checkedListNode="checkedListNode"
        :arrt="arrt"
        @savePersonHandle="savePersonHandle"
        ref="singleNode"
      ></tranformcom>
      <div class="btnbox">
        <el-button @click="drawer = false">关闭</el-button>
        <el-button type="primary" @click="SubmitRoles">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import tranformcom from '@/components/custom/singleNode.vue'
import { filterTreeHandeler } from '@/utils/tools/funtctionAPI.js'
import { flatPidToTree } from '@/utils/tools/common'
export default {
  components: { tranformcom },
  props: {
    userRow: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      overallId: 'id',
      RolesList: [],
      drawer: false,
      updateList: null,
      id: '',
      loading: false,
      arrt: {},
      submitList: [],
      list: [],
      checkedListNode: []
    }
  },
  created() {},
  mounted() {},
  methods: {
    SubmitRoles() {
      let list = []
      if (this.submitList && this.submitList.length < 1) {
        this.submitList = this.$refs.singleNode.checkedList
        this.submitList.forEach((item) => {
          list.push({
            UserID: item.userId,
            // realName: item.realName,
            // userName: item.userName,
            sysOrganizeId: item.sysOrganizeId,
            sysDutyId: item.sysDutyId,
            // sysOrganizeName: item.sysOrganizeName,
            // sysDutyName: item.sysDutyName,
            // sysAdministratRankName: item.sysAdministratRankName,
            sysAdministratRankId: item.sysAdministratRankId,
            sysOrganizeType: item.sysOrganizeType,
            mobil: item.mobil
          })
        })
      } else {
        this.submitList.forEach((item) => {
          if (item.userId !== undefined) {
            item.overallUserID = item.userId
          }
          if (item.name !== undefined) {
            item.overallUserName = item.name
          }
          if (item.realName !== undefined) {
            item.overallRealName = item.realName
          }
          if (item.sysDutyId !== undefined) {
            item.overallDutyId = item.sysDutyId
          }
          if (item.sysOrganizeId !== undefined) {
            item.overallOrganizeId = item.sysOrganizeId
          }
          list.push(
            item.overallOrganizeId + ',' + item.overallDutyId + ',' + item.overallUserID
            //   {
            //   UserID: item.overallUserID,
            //   // realName: item.overallRealName,
            //   // userName: item.overallUserName,
            //   sysOrganizeId: item.overallOrganizeId,
            //   sysDutyId: item.overallDutyId,
            //   // sysOrganizeName: item.overallOrganizeName,
            //   // sysDutyName: item.overallDutyName,
            //   // sysAdministratRankName: item.overallAdministratRankName,
            //   sysOrganizeId: item.overallOrganizeId,
            //   sysAdministratRankId: item.overallAdministratRankId,
            //   sysOrganizeType: item.overallOrganizeType,
            //   mobil: item.overallUserMobile,
            // }
          )
        })
      }
   
      this.authorityApi.DirectSupervisor.save(
        {
          directSupervisorInfos: list.join(';'),
          sysOrganizeId: this.userRow.organizeId,
          sysDutyId: this.userRow.dutyId,
          sysUserId: this.userRow.userId
        },
        (res) => {
          this.$message.success(res)
          this.drawer = false
        }
      )
    },
    CloseRoles() {
      this.RolesList = []
      this.list = []
      this.$refs.singleNode.clearhandler()
      this.submitList = []
    },
    savePersonHandle(list) {
      this.submitList = list
    },
    openhander() {
      this.loading = true
      let data = {
        queryType: 5
      }
      this.authorityApi.UserPort.getOrganizeAllUser(data, (res) => {
        this.loading = false
        this.list = flatPidToTree(res)
        filterTreeHandeler(this.list)
        // this.list[0].children.forEach(item=>{
        //   this.checkedListNode.push(item.id)
        // })
        this.checkedListNode.push(this.list[0].id)
        if (this.list && this.list.length > 0) {
          this.authorityApi.DirectSupervisor.getByUserInfo(
            {
              sysOrganizeId: this.userRow.organizeId,
              sysDutyId: this.userRow.dutyId,
              id: this.userRow.userId
            },
            (res) => {
              if (res == 'no content') {
                this.$refs.singleNode.checkedList = []
              } else {
                res.forEach((item) => {
                  item.name = item.realName
                  item.overallDutyName = item.sysDutyName
                  item.overallOrganizeName = item.sysOrganizeName
                })
                // res.forEach(item => {
                //   item.id = item.userId;
                // });
                let list = res.map((item) => item.userId)
                this.$refs.singleNode.setTreeChecked(list)
                this.$refs.singleNode.rightHandler()
              }
            }
          )
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.table {
  width: 100%;
  font-size: 12px;
}
/deep/.btnbox {
  margin-top: 20px;
  text-align: center;
  .el-button {
    font-size: 12px;
  }
}
</style>
